﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Scheduler2;
using System.Web.UI;

namespace Scheduler_Day_Adley_Hubbard
{
    public partial class schedulerForm : Form
    {
        SqlConnection cn = new SqlConnection("Data Source=(local);Integrated Security=True");
        SqlCommand cmd = new SqlCommand();

        ListViewItem item;
        

        public schedulerForm()
        {
            InitializeComponent();
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            cmd.Connection = cn;
            todaysDateLabel.Text = DateTime.Today.ToString("D");
            startDatePicker.CustomFormat = "MM/dd/yyyy hh:mm:ss tt";
            startDatePicker.Format = DateTimePickerFormat.Custom;

            endDatePicker.CustomFormat = "MM/dd/yyyy hh:mm:ss tt";
            endDatePicker.Format = DateTimePickerFormat.Custom;

            editButton.Visible = false;
            panel1.Visible = true;
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            monthCalendar1.MaxSelectionCount = 1;

            DateTime date = monthCalendar1.SelectionRange.Start;
            string beginday = date.Month + "/" + date.Day + "/" + date.Year;

            startDatePicker.Value = date;
            endDatePicker.Value = date.AddHours(1);

            cn.Open();
            string query = "SELECT Title, StartDate, Notes FROM Task WHERE StartDate BETWEEN '" + beginday + "' and '" + beginday + " 23:59:59'";
            using (SqlCommand cmd = new SqlCommand(query, cn))
            {
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    List<tableRow> items = new List<tableRow>();
                    listView1.Items.Clear();



                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem("Title");
                        item.SubItems.Add("Start");
                        item.SubItems.Add("Notes");
                        listView1.Items.Add(item);
                        tableRow tr = new tableRow();
                        tr.one = reader[0].ToString();
                        tr.two = reader[1].ToString().Substring(10);
                        tr.three = reader[2].ToString();
                        items.Add(tr);
                    }

                    for (int i = 0; i < items.Count(); i++)
                    {
                        tableRow t = items[i];
                        listView1.Items[i].SubItems[0].Text = items[i].one;
                        listView1.Items[i].SubItems[1].Text = items[i].two;
                        listView1.Items[i].SubItems[2].Text = items[i].three;

                    }


                }
            }
            cn.Close();

            todaysDateLabel.Text = date.ToString("D");




        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Save button clicked
            DateTime dateValue;
            if (DateTime.TryParse(startDatePicker.Text, out dateValue) || DateTime.TryParse(endDatePicker.Text, out dateValue))
            {
                if (endDatePicker.Value > startDatePicker.Value)
                {
                    cmd.CommandText = "Insert into Task (StartDate, Title, Notes, EndDate) values (@StartDate, @Title, @Notes, @EndDate)";
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = cn;
                    cmd.Parameters.AddWithValue("@StartDate", startDatePicker.Text);
                    cmd.Parameters.AddWithValue("@Title", titleBox.Text);
                    cmd.Parameters.AddWithValue("@Notes", notesBox.Text);
                    cmd.Parameters.AddWithValue("@EndDate", endDatePicker.Text);

                    cn.Open();

                    cmd.ExecuteNonQuery();
                    cmd.Clone();
                    MessageBox.Show("Record inserted!", "");
                    cn.Close();
                    cmd.Parameters.Clear();

                    panel1.Visible = false;
                    panel2.Visible = true;
                }
                else
                {
                    MessageBox.Show("End date was set before start date", "Impossible Event Time", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
            else
            {
                MessageBox.Show("Date not entered correctly", "Impossible Event Time", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void progressBar1_Click(object sender, EventArgs e)
        {

        }

        private void notesBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void tableBindingSource_CurrentChanged(object sender, EventArgs e)
        {
        }

        private void button_Click(object sender, EventArgs e)
        {
            //Add button

            //Switch to add screen
            panel2.Visible = false;
            panel1.Visible = true;

            editButton.Visible = false;
            saveButton.Visible = true;

            //Clear add screen fields
            titleBox.Text = "";
            notesBox.Text = "";


        }

        private void editButton_Click(object sender, EventArgs e)
        {
           


        }


        private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {

        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {
            monthCalendar1.MaxSelectionCount = 1;

            DateTime date = monthCalendar1.SelectionRange.Start;
            string beginday = date.Month + "/" + date.Day + "/" + date.Year;

            cn.Open();
            string query = "SELECT Title, StartDate, EndDate, Notes, Id FROM Task WHERE StartDate BETWEEN '" + beginday + "' and '" + beginday + " 23:59:59'";
            using (SqlCommand cmd = new SqlCommand(query, cn))
            {
                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    List<tableRow> items = new List<tableRow>();
                    listView1.Items.Clear();                                      

                    // Reads the database and puts information into tablerow class
                    while (reader.Read())
                    {
                        ListViewItem item = new ListViewItem("Title");
                        item.SubItems.Add("Start");
                        item.SubItems.Add("End");
                        item.SubItems.Add("Notes");
                        item.SubItems.Add("Id");
                        listView1.Items.Add(item);
                        tableRow tr = new tableRow();
                        tr.one = reader[0].ToString();
                        tr.two = reader[1].ToString();
                        tr.three = reader[2].ToString();
                        tr.four = reader[3].ToString();
                        tr.id = reader[4].ToString();
                        items.Add(tr);
                    }

                    // Gets infomration from table row class and puts it in ListView
                    for (int i = 0; i < items.Count(); i++)
                    {
                        tableRow t = items[i];
                        listView1.Items[i].SubItems[0].Text = items[i].one;
                        listView1.Items[i].SubItems[1].Text = items[i].two;
                        listView1.Items[i].SubItems[2].Text = items[i].three;
                        listView1.Items[i].SubItems[3].Text = items[i].four;
                        listView1.Items[i].SubItems[4].Text = items[i].id;

                    }
                }
            }
            cn.Close();

            todaysDateLabel.Text = date.ToString("D");

        }

        private void taskDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Clicked on item in listview

            //listview item was selected
            if (listView1.SelectedItems.Count > 0)
            {
                
                item = listView1.SelectedItems[0];
                //Switch to edit screen
                panel2.Visible = false;
                panel1.Visible = true;

                string start = item.SubItems[1].Text;
                string end = item.SubItems[2].Text;
                DateTime dateValue;
                if (DateTime.TryParse(start, out dateValue) || DateTime.TryParse(end, out dateValue))
                {
                    // Sets the text boxes for edit
                    titleBox.Text = item.Text;
                    startDatePicker.Value = DateTime.Parse(start);
                    endDatePicker.Value = DateTime.Parse(end);
                    notesBox.Text = item.SubItems[3].Text;

                    editButton.Visible = true;
                    saveButton.Visible = false;
                }
                else
                {
                    MessageBox.Show("All items need to be ", "Impossible Event Time", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
        }

        private void todaysDateLabel_Click(object sender, EventArgs e)
        {

        }

        private void cancel_Click(object sender, EventArgs e)
        {
            panel2.Visible = true;
            panel1.Visible = false;
        }

        private void editButton_Click_1(object sender, EventArgs e)
        {
            if (endDatePicker.Value > startDatePicker.Value)
            {
                //Edit Information with tabel primary key -> id
                int id = int.Parse(item.SubItems[4].Text);
                cmd.CommandText = "UPDATE Task SET StartDate = @StartDate, Title = @Title, Notes = @Notes, EndDate = @EndDate WHERE Id = @Id";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = cn;
                cmd.Parameters.AddWithValue("@Id", id);
                cmd.Parameters.AddWithValue("@StartDate", startDatePicker.Text);
                cmd.Parameters.AddWithValue("@Title", titleBox.Text);
                cmd.Parameters.AddWithValue("@Notes", notesBox.Text);
                cmd.Parameters.AddWithValue("@EndDate", endDatePicker.Text);
               
                cn.Open();

                cmd.ExecuteNonQuery();
                cmd.Clone();
                MessageBox.Show("Record Updated!", "");
                cn.Close();
                cmd.Parameters.Clear();

                panel2.Visible = true;
                panel1.Visible = false;

            }else{
                    MessageBox.Show("End date was set before start date", "Impossible Event Time", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }
    }
}
